package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.ProjectManagement;
import com.mxpio.erp.common.equipment.entity.ProjectStatusRecord;
import com.mxpio.erp.common.equipment.service.ProjectManagementService;
import com.mxpio.erp.common.equipment.service.ProjectStatusRecordService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "ProjectStatusRecordController", description = "状态记录")
@RestController
@RequestMapping("/erp/equipment/project/status/record/")
public class ProjectStatusRecordController {

    @Autowired
    private ProjectStatusRecordService projectStatusRecordService;

    @Autowired
    private ProjectManagementService projectManagementService;

    @GetMapping("list")
    @Operation(summary = "状态记录", description = "获取状态记录(无分页)", method = "GET")
    public Result<List<ProjectStatusRecord>> list(@Parameter(description="查询参数")Criteria criteria){
        List<ProjectStatusRecord> list = projectStatusRecordService.list(ProjectStatusRecord.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "状态记录", description = "获取状态记录(分页)", method = "GET")
    public Result<Page<ProjectStatusRecord>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        PageRequest page = PageRequest.of(pageNo - 1, pageSize);
        Page<ProjectStatusRecord> listPage = projectStatusRecordService.listPage(ProjectStatusRecord.class, page, criteria);
        return Result.OK(listPage);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取设备状态列表", description = "根据ID获取设备状态列表", method = "GET")
    public Result<ProjectStatusRecord> getById(@PathVariable(name = "id", required = true)String id){
        ProjectStatusRecord byId = projectStatusRecordService.getById(ProjectStatusRecord.class, id);
        return Result.OK(byId);
    }

    @RequestMapping("add")
    @Operation(summary = "保存设备状态", description = "保存设备状态", method = "POST")
    public Result<ProjectStatusRecord> add(@RequestBody ProjectStatusRecord projectStatusRecord){
        ProjectStatusRecord save = projectStatusRecordService.save(projectStatusRecord);
        return Result.OK(save);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑设备状态", description = "编辑设备状态", method = "PUT")
    public Result<ProjectStatusRecord> edit(@RequestBody ProjectStatusRecord projectStatusRecord){
        ProjectStatusRecord update = projectStatusRecordService.update(projectStatusRecord);
        return Result.OK(update);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除设备状态", description = "删除设备状态,多个id用英文逗号分隔", method = "DELETE")
    public Result<ProjectStatusRecord> remove(@PathVariable(name = "ids", required = true)String ids){
        String[] id = ids.split(",");
        for (String key : id) {
            projectStatusRecordService.delete(ProjectStatusRecord.class, key);
        }
        return Result.OK();
    }

    @PutMapping("listExecute/{proCode}")
    public Result<ProjectManagement> listExecute(@PathVariable(name = "proCode", required = true)String proCode){
        ProjectManagement pm = projectManagementService.getById(ProjectManagement.class, proCode);
        projectManagementService.listExecute(pm);
        return Result.OK();
    }
}
